﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Exercitation.Model;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.Account.Model;
using Yipusoft.Common.Model;
using System.Drawing;

public partial class Combat_InputScore : System.Web.UI.Page
{
    TextBox txtCount;
    RadioButtonList rdioList;
    private Control _survey = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }
    private void Bind()
    {
        lblTitle.Text = "待录入士兵";
        UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        if (user == null)
            return;
        List<TaskSoldiersInfo> soldierList = TaskSoldiers.GetList(" DepartmentID='" + user.DepartmentID + "' And TaskID='" + Request["TaskID"] + "'");
        Soldier soldier = new Soldier();
        dltSoldiers.DataSource = soldier.GetSoldiersByWhere(" SoldierID IN (Select SoldierID From FireMis..TaskSoldiers Where  DepartmentID='" + user.DepartmentID + "' And TaskID='" + Request["TaskID"] + "' Group by SoldierID)", "SoldierID", "Asc", 9999, 0);
        //Soldier soldier = new Soldier();       
        //List<SoldierArchiveInfo> soldierList = soldier.GetSoldiersByWhere(" DepartmentID='" + user.DepartmentID + "' And IsDelete='0'", "SoldierID", "Desc", 1000, 0);
        //List<TaskItemsInfo> taskitemList = TaskItems.GetList(" TaskID='" + Request["TaskID"] + "'");
        //List<SoldierArchiveInfo> dataList = new List<SoldierArchiveInfo>();
        //foreach (SoldierArchiveInfo info in soldierList)
        //{
        //    SoldierRankInfo rankInfo = SoldierRank.GetSoldierRank(info.SoldierID);            
        //    if (rankInfo != null)
        //    {
        //        List<SoldierStationInfo> stationList = SoldierStation.GetList("  SoldierID='" + info.SoldierID + "'");
        //        foreach (SoldierStationInfo s in stationList)
        //        {
        //            if (taskitemList.Where(o => o.StationID == s.StationID && o.RankID == rankInfo.RankID).Count() > 0)
        //            {
        //                if (dataList.Where(o => o.SoldierID == info.SoldierID).Count() <= 0)
        //                {
        //                    dataList.Add(info);
        //                }

        //            }
        //        }
        //    }
        //}
        //dltSoldiers.DataSource = dataList;
        //dltSoldiers.DataBind();

        //dltSoldiers.DataSource = soldierList;
        dltSoldiers.DataBind();
    }

    public string GetSoldierName(object soldierID)
    {
        if (soldierID == null)
            return "";

        Yipusoft.SoldierArchives.BLL.Soldier soldier = new Soldier();
        return soldier.GetSoldierInfoByID(soldierID.ToString()).SoldierName;
    }
    protected void lnbtnSoldier_Command(object sender, CommandEventArgs e)
    {
        lblTitle2.Text = "【" + ((LinkButton)sender).Text + "】 成绩录入";
        Soldier soldier = new Soldier();
        SoldierArchiveInfo info = soldier.GetSoldierInfoByID(e.CommandArgument.ToString());
        if (info == null)
            return;
        lblRank.Text = "";

        // SoldierRankInfo rankInfo = SoldierRank.GetSoldierRank(info.SoldierID);
        TaskSoldiersInfo soldierInfo = TaskSoldiers.GetTaskSoldiers(info.SoldierID.ToString());
        lblRank.Text = Yipusoft.Common.BLL.CodeItem.GetCodeItem(soldierInfo.RankID).ItemName;

        List<CodeItemInfo> itemList = Yipusoft.Common.BLL.CodeItem.GetList("ItemID IN (Select StationID From TaskSoldiers Where SoldierID='" + info.SoldierID + "')");
        lblStation.Text = "";
        foreach (CodeItemInfo code in itemList)
        {
            lblStation.Text += code.ItemName + "、";
        }
        btnSave.CommandArgument = e.CommandArgument.ToString();
        dltSkill.DataSource = TaskSkill.GetList(" SkillID IN(Select SkillID From TaskSubject Where  TaskID='" + Request["TaskID"] + "' And TaskItemID IN ( Select ID From TaskItems Where TaskID ='" + Request["TaskID"] + "' And RankID='" + soldierInfo.RankID + "' And StationID IN (Select StationID From TaskSoldiers Where SoldierID='" + info.SoldierID + "')))");
        dltSkill.DataBind();

    }
    protected void dltSkill_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        Label lblSkillID = e.Item.FindControl("lblSkill") as Label;
        Label lblType = e.Item.FindControl("lblSkillType") as Label;
        Label lblStandard = e.Item.FindControl("lblStandard") as Label;
        Soldier soldier = new Soldier();
        SoldierArchiveInfo info = soldier.GetSoldierInfoByID(btnSave.CommandArgument);
        TaskSkillInfo skillInfo = TaskSkill.GetTaskSkill(new Guid(lblSkillID.Text));

        if (skillInfo != null && lblStandard != null)
        {
            if (!lblType.Text.Equals("2"))
            {
                lblStandard.Text = skillInfo.SkillStandard.ToString();
            }
            else
            {
                lblStandard.Text = DateTime.Parse(skillInfo.SkillStandard).ToString("HH时mm分ss秒");
            }
        }
        TaskScoreInfo scoreInfo = null;
        if (info != null)
        {
            scoreInfo = TaskScore.GetList(" TaskID='" + Request["TaskID"] + "' And DepartmentID='" + info.DepartmentID + "' And SoldierID='" + info.SoldierID + "' And SkillID='" + lblSkillID.Text + "'").FirstOrDefault();
        }


        if (lblType.Text.Equals("3"))
        {

            TextBox txt = e.Item.FindControl("txtCount") as TextBox;
            RangeValidator range = e.Item.FindControl("RangeValidator1") as RangeValidator;
            RequiredFieldValidator empty = e.Item.FindControl("RequiredFieldValidator1") as RequiredFieldValidator;

            if (empty != null)
            {
                empty.Enabled = true;
            }
            if (range != null)
            {
                range.Enabled = true;
            }
            txt.Visible = true;
            if (info != null)
            {

                if (scoreInfo != null)
                {
                    txt.Text = scoreInfo.Score;
                }
            }
        }
        else if (lblType.Text.Equals("1"))
        {
            RadioButtonList ralist = e.Item.FindControl("rbtn") as RadioButtonList;
            ralist.Visible = true;
            if (scoreInfo != null)
            {
                ralist.SelectedValue = scoreInfo.Score;
            }

        }
        else if (lblType.Text.Equals("2"))
        {
            DropDownList ddlHour = e.Item.FindControl("ddlHour") as DropDownList;
            ddlHour.Visible = true;
            DropDownList ddlMinus = e.Item.FindControl("ddlMinus") as DropDownList;
            ddlMinus.Visible = true;
            DropDownList ddlSec = e.Item.FindControl("ddlSec") as DropDownList;
            ddlSec.Visible = true;
            DropDownList ddlMinsec = e.Item.FindControl("ddlMinsec") as DropDownList;
            ddlMinsec.Visible = true;

            for (int i = 0; i <= 12; i++)
            {
                ddlHour.Items.Add(new ListItem(i.ToString() + "小时", i.ToString()));
            }
            for (int i = 0; i < 60; i++)
            {
                ddlMinus.Items.Add(new ListItem(i.ToString() + "分", i.ToString()));
            }
            for (int i = 0; i < 60; i++)
            {
                ddlSec.Items.Add(new ListItem(i.ToString() + "秒", i.ToString()));
            }
            for (int i = 0; i < 1000; i++)
            {
                ddlMinsec.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            if (scoreInfo != null)
            {
                DateTime dt = DateTime.Parse(scoreInfo.Score);
                ddlHour.SelectedValue = int.Parse(dt.ToString("HH")).ToString();
                ddlMinus.SelectedValue = int.Parse(dt.ToString("mm")).ToString();
                ddlSec.SelectedValue = int.Parse(dt.ToString("ss")).ToString();
                ddlMinsec.SelectedValue = int.Parse(dt.ToString("fff")).ToString();
            }

        }

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
         if (string.IsNullOrEmpty(btnSave.CommandArgument))
        {
            lblPrompt.Text = "请选择要录入的人员";
            return;
        }
        TaskBaseInfo taskInfo = TaskBase.GetTaskBase(new Guid(Request["TaskID"].ToString()));
        lblPrompt.Text = "";
        if (TaskSoldierScore.GetCount("SoldierID='" + btnSave.CommandArgument + "' And Score>1") <= 0)
        {
            if (taskInfo.EndDate < DateTime.Now)
            {
                lblPrompt.Text = "此任务已过期！";
                return;
            }
        }
       
        Soldier soldier = new Soldier();

        //下达任务时士兵所在单位
        string DepartmentID = TaskSoldiers.GetList(" TaskID='" + Request["TaskID"] + "' And SoldierID='" + btnSave.CommandArgument + "'").FirstOrDefault().DepartmentID;

        SoldierArchiveInfo info = soldier.GetSoldierInfoByID(btnSave.CommandArgument); //士兵信息
        if (info == null)
            return;

        SoldierRankInfo rankInfo = SoldierRank.GetSoldierRank(info.SoldierID); //警衔
        if (rankInfo == null)
            return;

        decimal passCount = 0;
        foreach (DataListItem item in dltSkill.Items)
        {
            //技能ID
            string skillID = ((Label)item.FindControl("lblSkill")).Text;

            //技能类型
            string skillType = ((Label)item.FindControl("lblSkillType")).Text;

            //技能标准
            TaskSkillInfo skillInfo = TaskSkill.GetTaskSkill(new Guid(skillID));
           
            //获取下达的技能（有多个岗位可能有重复技能）
            List<TaskSubjectInfo> subList = TaskSubject.GetList(" TaskItemID IN (Select ID From TaskItems Where TaskID='" + Request["TaskID"] + "' And RankID='" + rankInfo.RankID + "' And StationID IN (Select StationID From SoldierStation Where SoldierID='" + info.SoldierID + "') ) And SkillID='" + skillID + "'");

            foreach (TaskSubjectInfo subinfo in subList)
            {
                string sc = string.Empty;
                if (skillType.Equals("2"))
                {
                    DropDownList ddlHour = item.FindControl("ddlHour") as DropDownList;
                    DropDownList ddlMinus = item.FindControl("ddlMinus") as DropDownList;
                    DropDownList ddlSec = item.FindControl("ddlSec") as DropDownList;
                    DropDownList ddlMinsec = item.FindControl("ddlMinsec") as DropDownList;
                    sc = new DateTime(2010, 8, 8, int.Parse(ddlHour.SelectedValue), int.Parse(ddlMinus.SelectedValue), int.Parse(ddlSec.SelectedValue), int.Parse(ddlMinsec.SelectedValue)).ToString("yyyy-MM-dd HH:mm:ss.fff");
                    DateTime dtstarded = DateTime.Parse(skillInfo.SkillStandard);
                    if (dtstarded>=DateTime.Parse(sc))
                    {
                        passCount += 1;
                    }
                }
                else if (skillType.Equals("3"))
                {
                    //Response.Write(skillID);
                    //return ;
                    sc = ((TextBox)item.FindControl("txtCount")).Text;
                    if (int.Parse(sc) >= int.Parse(skillInfo.SkillStandard))
                    {
                        passCount += 1;
                    }
                }
                else if (skillType.Equals("1"))
                {
                    RadioButtonList raList = item.FindControl("rbtn") as RadioButtonList;
                    sc = raList.SelectedValue;
                    if (sc.Equals(skillInfo.SkillStandard))
                    {
                        passCount += 1;
                    }
                }


                TaskScoreInfo socre = new TaskScoreInfo();
                socre.DepartmentID = DepartmentID;//info.DepartmentID;
                socre.Score = sc;
                socre.SkillID = new Guid(skillID);
                socre.SoldierID = new Guid(info.SoldierID);
                socre.TaskID = new Guid(Request["TaskID"]);
                socre.TaskScoreID = Guid.NewGuid();

                List<TaskScoreInfo> scoreList = TaskScore.GetList(" TaskID='" + Request["TaskID"] + "' And DepartmentID='" + info.DepartmentID + "' And SoldierID='" + info.SoldierID + "' And SkillID='" + skillID + "'");
                if (scoreList == null || scoreList.Count <= 0)
                {
                    TaskScore.Insert(socre);
                }
                else
                {
                    //如果数据库里面存在同一任务同一士兵同一训练技能则只录入一次更新全部
                    foreach (TaskScoreInfo s in scoreList)
                    {
                        socre.TaskScoreID = s.TaskScoreID;
                        socre.DepartmentID = s.DepartmentID;
                        socre.SkillID = s.SkillID;
                        socre.SoldierID = s.SoldierID;
                        socre.TaskID = s.TaskID;
                        socre.Score = sc;
                        TaskScore.Update(socre);
                    }
                }

            }
        }

        int count= TaskSubject.GetCount("TaskItemID IN (Select ID From TaskItems Where TaskID='" + Request["TaskID"] + "' And RankID='" + rankInfo.RankID + "' And StationID IN (Select StationID From SoldierStation Where SoldierID='" + info.SoldierID + "') )");
        TaskSoldierScoreInfo score = new TaskSoldierScoreInfo();
        score.ID = Guid.NewGuid();
        score.TaskDate = taskInfo.EndDate;
        score.SoldierID = new Guid(info.SoldierID);
        score.CardID = info.CardID;
        score.DepartmentID = DepartmentID;
        score.SetDate = DateTime.Now;
        score.TaskID = taskInfo.TaskID;
        score.Score = Math.Round((passCount / count), 2);
        TaskSoldierScoreInfo tInfo = TaskSoldierScore.GetList("SoldierID='" + info.SoldierID + "'And CardID='" + info.CardID + "' And TaskDate='" + taskInfo.EndDate + "'").FirstOrDefault();

        if (tInfo != null)
        {
            score.ID = tInfo.ID;
            TaskSoldierScore.Update(score);
        }
        else
        {
            TaskSoldierScore.Insert(score);
        }
        Bind();
        btnSave.CommandArgument = "";
    }
    protected void dltSoldiers_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton btn = e.Item.FindControl("lnbtnSoldier") as LinkButton;
        if (btn == null)
            return;
        int count = TaskScore.GetCount(" TaskID='" + Request["TaskID"] + "' And SoldierID='" + btn.CommandArgument + "'");
        if (count > 0)
            btn.ForeColor = Color.Green;
    }
}
